# -*- coding: utf-8 -*-

from telethon import TelegramClient, utils
import config

client = TelegramClient(config.s_name, config.app_id, config.app_hash, proxy=("socks5", '127.0.0.1', 7890), timeout=60)


async def get_dialogs():
    dialogs = await client.get_dialogs()
    channels = [dialog for dialog in dialogs if dialog.is_channel]
    dialog_list = []
    count = 0
    for dialog in channels:
        count += 1
        real_id, peer_type = utils.resolve_id(dialog.id)
        # print(dialog)
        if dialog.is_user:
            c_type = 1
        elif dialog.is_group:
            c_type = 2
        elif dialog.is_channel:
            c_type = 3
        print(f"ID: {count}, Channel ID: {real_id}, Channel Name: {dialog.title}, Channel Type: {c_type}")
        dialog_list.append({
            'id': count,
            'c_id': real_id,
            'c_name': dialog.title,
            'c_type': c_type
        })
    # print(dialog_list)
    return dialog_list


def get_messages():
    client.iter_messages()


async def main():
    await client.start(config.p_no)
    await get_dialogs()
    await client.disconnect()
    # client.iter_messages()


if __name__ == '__main__':
    import asyncio

    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    except Exception as e:
        print(f"An error occurred: {e}")